﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Micro.SMSGatWay.Core.Entity;
using Micro.SMSGatWay.Core.Model.Req.APIModel;

namespace Micro.SMSGatWay.Core.Service
{
    public class SmsService
    {
        protected readonly SmsAppConfig _smsAppConfig;
        public SmsService(SmsAppConfig smsAppConfig)
        {
            _smsAppConfig = smsAppConfig;
        }
        /// <summary>
        /// 替换阿里云参数模板
        /// </summary>
        /// <param name="reqSendSimpleSms"></param>
        /// <returns></returns>
        protected Dictionary<string, string> TransForALiYunParams(ReqSmsSend reqSmsSend)
        {
            Dictionary<string, string> paramS = new Dictionary<string, string>();
            if (reqSmsSend.Code != null)
                paramS.Add("code", reqSmsSend.Code);
            if (reqSmsSend.Name != null)
                paramS.Add("Name", reqSmsSend.Name);
            if (reqSmsSend.Param1 != null)
                paramS.Add("Param1", reqSmsSend.Param1);
            if (reqSmsSend.Param2 != null)
                paramS.Add("Param2", reqSmsSend.Param2);
            if (reqSmsSend.Param3 != null)
                paramS.Add("Param3", reqSmsSend.Param3);
            if (reqSmsSend.Param4 != null)
                paramS.Add("Param4", reqSmsSend.Param4);
            if (reqSmsSend.Param5 != null)
                paramS.Add("Param5", reqSmsSend.Param5);
            if (reqSmsSend.Param6 != null)
                paramS.Add("Param6", reqSmsSend.Param6);
            return paramS;
        }
        /// <summary>
        /// 替换云片模板参数
        /// </summary>
        /// <param name="reqSendSimpleSms"></param>
        /// <returns></returns>
        protected string TransForYunPParams(ReqSmsSend reqSmsSend)
        {
            string tplValue = "";
            if (reqSmsSend.Code != null)
                tplValue += System.Web.HttpUtility.UrlEncode("&#code#") + "=" + System.Web.HttpUtility.UrlEncode(reqSmsSend.Code);
            if (reqSmsSend.Name != null)
                tplValue += System.Web.HttpUtility.UrlEncode("&#Name#") + "=" + System.Web.HttpUtility.UrlEncode(reqSmsSend.Name);
            if (reqSmsSend.Param1 != null)
                tplValue += System.Web.HttpUtility.UrlEncode("&#Param1#") + "=" + System.Web.HttpUtility.UrlEncode(reqSmsSend.Param1);
            if (reqSmsSend.Param2 != null)
                tplValue += System.Web.HttpUtility.UrlEncode("&#Param2#") + "=" + System.Web.HttpUtility.UrlEncode(reqSmsSend.Param2);
            if (reqSmsSend.Param3 != null)
                tplValue += System.Web.HttpUtility.UrlEncode("&#Param3#") + "=" + System.Web.HttpUtility.UrlEncode(reqSmsSend.Param3);
            if (reqSmsSend.Param4 != null)
                tplValue += System.Web.HttpUtility.UrlEncode("&#Param4#") + "=" + System.Web.HttpUtility.UrlEncode(reqSmsSend.Param4);
            if (reqSmsSend.Param5 != null)
                tplValue += System.Web.HttpUtility.UrlEncode("&#Param5#") + "=" + System.Web.HttpUtility.UrlEncode(reqSmsSend.Param5);
            if (reqSmsSend.Param6 != null)
                tplValue += System.Web.HttpUtility.UrlEncode("&#Param6#") + "=" + System.Web.HttpUtility.UrlEncode(reqSmsSend.Param6);
            return tplValue;
        }

        protected string BuildOldTemplate(SmsSignInfo smsSignInfo, SmsTemplateInfo smsTemplateInfo, ReqSmsSend reqSendSimpleSms)
        {
            //替换发送内容
            string templateOldContent = "【" + smsSignInfo.SignName + "】" + smsTemplateInfo.TemplateContent;
            if (!string.IsNullOrEmpty(reqSendSimpleSms.Code))
                templateOldContent = templateOldContent.Replace("{code}", reqSendSimpleSms.Code);
            if (!string.IsNullOrEmpty(reqSendSimpleSms.Name))
                templateOldContent = templateOldContent.Replace("{Name}", reqSendSimpleSms.Name);
            if (!string.IsNullOrEmpty(reqSendSimpleSms.Param1))
                templateOldContent = templateOldContent.Replace("{Param1}", reqSendSimpleSms.Param1);
            if (!string.IsNullOrEmpty(reqSendSimpleSms.Param2))
                templateOldContent = templateOldContent.Replace("{Param2}", reqSendSimpleSms.Param2);
            if (!string.IsNullOrEmpty(reqSendSimpleSms.Param3))
                templateOldContent = templateOldContent.Replace("{Param3}", reqSendSimpleSms.Param3);
            if (!string.IsNullOrEmpty(reqSendSimpleSms.Param4))
                templateOldContent = templateOldContent.Replace("{Param4}", reqSendSimpleSms.Param4);
            if (!string.IsNullOrEmpty(reqSendSimpleSms.Param5))
                templateOldContent = templateOldContent.Replace("{Param5}", reqSendSimpleSms.Param5);
            if (!string.IsNullOrEmpty(reqSendSimpleSms.Param6))
                templateOldContent = templateOldContent.Replace("{Param6}", reqSendSimpleSms.Param6);

            return templateOldContent;
        }
    }
}
